/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.misena.praxispraktik.recursos;

import com.misena.praxispraktik.entity.Empresarios;
import com.misena.praxispraktik.entity.Empresarios_;
import com.misena.praxispraktik.entity.Personas;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.ParameterExpression;
import javax.persistence.criteria.Root;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;

/**
 *
 * @author Avila
 */
@Stateless
@Path("/consulempresarioidper")
public class ConsulEmpresarioIdPer {
    EntityManagerFactory emf = null;
    EntityManager em = null;

    @GET
    @Produces("application/json")
    public Empresarios consultar(@QueryParam("idPersonas") Integer idPersonas) {
        try {
            emf = obtenerEntityManagerFactory();
            em = obtenerEntityManager(emf);
            
            Personas persona=null;
            persona = em.find(Personas.class, idPersonas);
            
            //CriteriaQuery<Usuarios> criteria = build.createQuery( Usuarios.class );
             CriteriaQuery criteria =em.getCriteriaBuilder().createQuery();
             Root<Empresarios> empresariosRoot = criteria.from( Empresarios.class );
             criteria.select( empresariosRoot );
             ParameterExpression<Personas> IdPersona = em.getCriteriaBuilder().parameter( Personas.class );
             criteria.where( em.getCriteriaBuilder().equal( empresariosRoot.get( Empresarios_.idPersonas), IdPersona ));
             TypedQuery<Empresarios> query = em.createQuery( criteria );
             query.setParameter( IdPersona, persona);
             return (Empresarios)query.getSingleResult();
            
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            em.close();
            emf.close();
        }
    }
    
    
    
     public EntityManagerFactory obtenerEntityManagerFactory() {

        return Persistence.createEntityManagerFactory("PraxisPraktikPU");
    }

    public EntityManager obtenerEntityManager(EntityManagerFactory emf) {

        return emf.createEntityManager();
    }
    
}
